package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.ScenicReviews;
import cn.wolfcode.luowowo.article.query.ScenicReviewsQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IScenicReviewsService;
import cn.wolfcode.luowowo.article.service.IScenicreviewsTagService;
import cn.wolfcode.luowowo.cache.service.IScenicCommentCacheService;
import cn.wolfcode.luowowo.cache.vo.ScenicStatisVo;
import cn.wolfcode.luowowo.comment.domain.ScenicComment;
import cn.wolfcode.luowowo.comment.service.IScenicCommentService;
import cn.wolfcode.luowowo.common.exception.LogicException;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.annotation.RequireLogin;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import cn.wolfcode.luowowo.website.util.UploadUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import javafx.fxml.LoadException;
import org.hibernate.validator.spi.scripting.ScriptEvaluatorNotFoundException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;


/**
 * 处理景点的接口
 */
@Controller
@RequestMapping("/scenic")
public class ScenicController {


    @Reference
    private IScenicReviewsService scenicService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IScenicreviewsTagService scenicreviewsTagService;
    @Reference
    private IScenicCommentService scenicCommentService;
    @Reference
    private IScenicCommentCacheService scenicCommentCacheService;


    //全部景点分页查询 异步请求分页
    @RequestMapping("/survey")
    public String survey(@ModelAttribute("qo") ScenicReviewsQuery qo, Model model) {
        //pageInfo
        PageInfo pageInfo = scenicService.queryForList(qo);
        model.addAttribute("pageInfo", pageInfo);

        return "scenic/listTpl";
    }

    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") ScenicReviewsQuery qo, Model model, Long destId, @UserParam UserInfo user) {
        //共享当前登录用户
        if (user != null) {
            model.addAttribute("user", user);
        }
        //根据吐司的destId查询出目的地信息
        model.addAttribute("dest", destinationService.selectByDestScenic(destId));
        //TOP5
        model.addAttribute("top5", scenicService.listTop5());
        //热门TOP10
        model.addAttribute("hot10", scenicService.listHot10());
        //景点标签
        model.addAttribute("tags", scenicreviewsTagService.selectAll());
        //全部景点
        model.addAttribute("all", scenicService.selectByScenic(destId));


        return "scenic/list";
    }
    //detail页面的内容
    @RequestMapping("detail")
    public String detail(Long id,Model model,@UserParam UserInfo userInfo){
        if(userInfo != null){
            model.addAttribute("user",userInfo);
        }
        ScenicReviews scenic = scenicService.getById(id);
        //单一景点查询
        model.addAttribute("scenic",scenic);
        //该景点的目的地的信息
        model.addAttribute("dest",scenic.getDest());
        //内部景点查询
        model.addAttribute("childs",scenicService.selectSonById(id));
        //查询真实评论数
        model.addAttribute("commentCount",scenicCommentService.getCommentCount(id));
        //2星以下
        model.addAttribute("negativeRatings",scenicCommentService.getCountByCategory(1,id));
        //3星中评 category=2
        model.addAttribute("average", scenicCommentService.getCountByCategory(2, id));
        //4到5星好评
        model.addAttribute("favorableComment", scenicCommentService.getCountByCategory(3, id));
        return "scenic/detail";
    }

    //评论添加
    @RequestMapping("commentAdd")
    @ResponseBody
    @RequireLogin
    public AjaxResult commentAdd(ScenicComment comment,@UserParam UserInfo userInfo){
        if(userInfo==null){
            throw new LogicException("请先登录");
        }
        AjaxResult result = new AjaxResult();
        try{
            comment.setCreateTime(new Date());
            comment.setUserId(userInfo.getId());
            comment.setLevel(userInfo.getLevel());
            comment.setHeadUrl(userInfo.getHeadImgUrl());
            comment.setCity(userInfo.getCity());
            comment.setUsername(userInfo.getNickname());
            scenicCommentService.commentAdd(comment);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("评论失败");
        }
        return result;
    }
    //评论添加(评论的评论)
    @RequestMapping("saveComment")
    @ResponseBody
    @RequireLogin
    public AjaxResult saveComment(ScenicComment comment,@UserParam UserInfo userInfo){
        if(userInfo==null){
            throw new LogicException("请先登录");
        }
        AjaxResult result = new AjaxResult();
        try{
            comment.setCreateTime(new Date());
            comment.setUserId(userInfo.getId());
            comment.setLevel(userInfo.getLevel());
            comment.setUsername(userInfo.getNickname());
            comment.setHeadUrl(userInfo.getHeadImgUrl());
            comment.setCity(userInfo.getCity());
            scenicCommentService.commentAdd(comment);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("评论失败b");
        }
        return result;
    }
    //评论显示
    @RequestMapping("/getComment")
    public Object getComment(@ModelAttribute("qo") ScenicReviewsQuery qo, Model model, Long scenicId) {
        //设置用户信息
        Page<ScenicComment> page = scenicCommentService.getComment(qo, scenicId);
        model.addAttribute("page", page);
        return "scenic/commentTpl";
    }
    //评论的图片上传
    @Value("${file.path}")
    private String filePath;

    @RequestMapping("/coverImageUpload")
    @ResponseBody
    public  Object uploadImg(MultipartFile pic){
        String filename = UploadUtil.upload(pic,filePath);
        return "/" + filename;
    }
    //评论分页
    @RequestMapping("/pageSearch")
    public Object pageSearch(@ModelAttribute("qo") ScenicReviewsQuery qo,Model model){
        Page<ScenicComment> page = scenicCommentService.getCommentByCategory(qo);
        model.addAttribute("page",page);
        return "scenic/commentTpl";
    }
    /**
     * 评论分类查询
     */
    @RequestMapping("/searchCategory")
    public String searchCategory(@ModelAttribute("qo") ScenicReviewsQuery qo,Model model){
        Page<ScenicComment> page = scenicCommentService.getCommentByCategory(qo);
        model.addAttribute("page",page);
        return "scenic/commentTpl";
    }
    /**
     * 评论点赞
     */
    @RequestMapping("/scenicThumbup")
    @ResponseBody
    public AjaxResult scenicThumbup(String id,@UserParam UserInfo userInfo){

        if(userInfo==null){
            AjaxResult result= new AjaxResult(false,"请先登录");
            result.setCode(102);
            return result;
        }
        Integer state = scenicCommentService.strategyThumbup(id,userInfo.getId());
        return new AjaxResult().addData(state);
    }
    /**
     * 收藏服务
     */
    @RequestMapping("/collect")
    @ResponseBody
    public AjaxResult collect(@UserParam UserInfo userInfo, Long scenicId){
        AjaxResult result = new AjaxResult();
        if(userInfo==null){
            result= new AjaxResult(false,"请先登录");
            result.setCode(102);
            return result;
        }
        boolean success = scenicCommentCacheService.collect(userInfo.getId(),scenicId);
        //设置收藏结果
        result.setSuccess(success);
        return result;
    }

    @RequestMapping("/havegoto")
    @ResponseBody
    public AjaxResult havegoto(@UserParam UserInfo userInfo, Long scenicId) {
        AjaxResult result = new AjaxResult();
        if(userInfo==null){
            result= new AjaxResult(false,"请先登录");
            result.setCode(102);
            return result;
        }
        boolean success = scenicCommentCacheService.havegoto(userInfo.getId(), scenicId);
        //设置收藏结果
        result.setSuccess(success);
        return result;
    }
}
